// import config from "@/config/index.js"
import store from '@/store/index.js'
import api from '@/api/flag.js'
import {
	sendMessage
} from '@/api/websocket.js'
import i18n from '@/i18n/index.js' // 国际化
import app from '@/main.js'
console.log(i18n)
// 去游戏页面
export async function goGamePang({
	id,
	name,
	namecode,
	gamepic
}) {
	try {
		let codeResult = await store.dispatch('app/getNameCode')
		console.log(codeResult)
		if (codeResult?.namecode && codeResult?.namecode !== namecode) {
			if (!store.state.app.recycleTimeSecond) {
				store.dispatch('app/getRecycle', false)
			}
			uni.showToast({
				title: i18n.t('game.recycle'),
				icon: 'none'
			})
			return
		}
		uni.showLoading({
			mask: true
		})
		sendMessage({
			flag: api.GAMEURL,
			content: {
				game_id: id
			}
		})
		uni.$once(api.GAMEURL, (res) => {
			if (res.code === 1) {
				let backPagePath = getCurrentPagePath()
				let {
					url,
					is_collect,
				} = res.data
				const gameParams = {
					id,
					name,
					namecode,
					gamepic,
					url,
					is_collect,
					backPagePath
				}
				uni.navigateTo({
					url: `/pages/public/game/game?gameParams=${encodeURIComponent(JSON.stringify(gameParams))}`
				});
				console.log('获取游戏链接', res.data.url);
			} else {
				uni.showToast({
					title: res.msg,
					icon: 'none'
				})
			}
			uni.hideLoading()
			// console.log('index,数巨巨巨巨巨',e.data)
		})
		// 进入游戏成功，更新账户余额
		// #ifdef H5
		// router.push({
		// 	path: '/pages/public/webviewH5',
		// 	query: {
		// 		webviewPath: encodeURI(res.data.url),
		// 		game_id,
		// 		is_collect: res.data.is_collect,
		// 	}
		// })
		// #endif

		// #ifdef APP-PLUS  
		// router.push({
		// 	path: '/pages/public/webviewH5',
		// 	query: {
		// 		webviewPath: encodeURI(res.data.url),
		// 		game_id,
		// 		is_collect: res.data.is_collect,
		// 	}
		// })
		// #endif
	} catch (e) {
		console.log('错误了？？？？', e)
		uni.hideLoading()
	}
}

// 获取游戏标识
function getGameFlag() {
	return new Promise((resolve, reject) => {
		resolve
	})
}


// 获取当前页面路由
export function getCurrentPagePath() {
	try {
		let page = getCurrentPages()
		return page[page.length - 1].route
	} catch (e) {
		console.log(e)
	}
}

// 退出游戏的拦截方法
export function exitGameInterception() {
	try {
		console.log('exitGameInterception')
	} catch (e) {
		console.log(e)
	}
}